package net.shop.biz.db.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import net.shop.biz.db.ProductService;
import net.shop.biz.service.FriendlyUrlService;
import net.shop.biz.vo.ProductVo;
import net.shop.common.ShopPager;
import net.shop.dal.dao.ProductDao;
import net.shop.dal.entity.Product;
import net.shop.dal.entity.ProductVirtualCategory;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product, Integer> implements ProductService {

	@Resource
	private ProductDao productDao;
	@Resource
	private FriendlyUrlService friendlyUrlService;
	
	public ShopPager<Product> getProductPager(ProductVirtualCategory category, ShopPager<Product> pager) {
		productDao.getVirtualCategoryProductList(category, pager);
		List<Product> products = pager.getResult();
		for (Product product : products) {
			product.setVo(getValueObject(category, product));
		}
		return pager;
	}
	
	public Product findProductByHaseCode(short languageId, long haseCode) {
		return productDao.findByHaseCode(languageId, haseCode);
	}
	
	private ProductVo getValueObject(ProductVirtualCategory parent, Product product) {
		ProductVo vo = new ProductVo();
		
		vo.setPicUrl(product.getProductPic());
		vo.setLinkUrl(friendlyUrlService.getProductUrl(parent, product));
		return vo;
	}
}
